
function add0(m){
	return m < 10 ? '0' + m:m
}

function getTimeLeft(datetimeTo){
  // 计算目标与现在时间差（毫秒）
  // let time1 = new Date(datetimeTo).getTime();
  let time1=datetimeTo;
  // let time2 = new Date().getTime();
  let time2=Date.parse(new Date())/1000;
  // let time2=Date.parse(new Date());
 let mss = time1 - time2;

   if(mss>0){
	   // 将时间差（毫秒）格式为：天时分秒
	   // let days = parseInt(mss / (1000 * 60 * 60 * 24));
	   if(mss>86400){
		   var ms=mss-86400;
       var day;
        //  var hours = parseInt(ms / ( 60 * 60))+24;
       var hours = parseInt(ms / (60 * 60)) + 24;
       console.log("parseInt", hours, "123", hours % 24)
       if(hours > 24){
         day = parseInt(hours / 24)
         hours = parseInt(ms / (60 * 60)) + 24 - (day * 24)
       }else{
         day = 0;
         hours = parseInt(ms / (60 * 60)) + 24;
       }
		   var minutes = parseInt(ms % ( 60 * 60) / 60);
		   var seconds = parseInt(ms %60);
		   // console.log(seconds)
		   // debugger
       console.log("day", day, "hours", hours)
       var time={
         type:1,
         day: day,
         hours: add0(hours),
         minutes: add0(minutes),
         seconds: add0(seconds)
       }
       return time
      //  return '剩余' + day+"天"  + add0(hours) + ":" + add0(minutes) + ":" + add0(seconds)  +'结束'
	   }else{
		   var hours = parseInt(mss  / ( 60 * 60));
		   var minutes = parseInt(mss % ( 60 * 60) / 60);
		   var seconds = parseInt(mss % 60);
       var time = {
         type: 1,
         day: 0,
         hours: add0(hours),
         minutes: add0(minutes),
         seconds: add0(seconds)
       }
       return time
		  //  return  '剩余' + add0(hours) + ":" + add0(minutes) + ":" + add0(seconds)  +'结束'
	   }
	  
   }else{
     var time = {
       type: 0,
       text: '已结束'
     }
     return time
   }
  
}


const util = require('../../utils/util.js');
import api from '../../utils/request/api'
import { specialGoodList  } from '../../utils/request/apiurl'


Page({
	data: {
		// datetimeTo: "2019/09/01 10:30:00 GMT+0800",
		datetimeTo:'',
		timeLeft: "" ,
		hour:'',
		micro_second:'',
		timer:'',
		// goodList: [{
		// 		id: 1,
		// 		img: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
		// 		title: '1蒙牛  纯甄 常温酸牛奶 青柠抹茶牛油果口味200g*16礼盒装',
		// 		oldPrice: 68.00,
		// 		newPrice: 36.60,
		// 		sales: 32515
		// 	}
		// ],
		goodList:[],
		isRequest: false,
		searchGood: {
		  page: 1,
		  size: 10,
		  pages:0
		},
		
	},
  //卸载页面
  onUnload: function () {
    clearInterval(this.data.timer);
  },
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		// countdown(this);
		this.specialGoodList();
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.data.timer = setInterval(() =>{ //注意箭头函数！！
	      this.setData({
	        timeLeft: getTimeLeft(this.data.datetimeTo) //getTimeLeft  
	      });
	      if (this.data.timeLeft == "0天0时0分0秒") {
	        clearInterval(this.data.timer);
			this.setData({
				timeLeft:'已结束'
			})
	      }
	    }, 1000);
	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
    
	  if (this.data.searchGood.page < this.data.searchGood.pages) {
		  this.setData({
		    searchGood: {
		      page: this.data.searchGood.page + 1,
		      size: 10,
		      pages: this.data.searchGood.pages
		    }
		  })
      this.specialGoodList()
	  }
	},
	// 获取商品列表
	// specialGoodList () {	 
	//   this.setData({
	//     isRequest: true
	//   })   
	// 	var source={
	// 		page:this.data.searchGood.page,
	// 		size:this.data.searchGood.size,
	// 	}
	//   api.post(specialGoodList.list,source).then(res => {
	// 	  // console.log(res)
	//     this.setData({
	//       goodList: res.rows,
	//     })
	// 	for(var i=0;i<res.rows.length;i++){
	// 		this.setData({
	// 			datetimeTo:res.rows[i].saleGoodsEndtime
	// 		})
	// 	}
	// 	var pages=res.total/this.data.searchGood.size
	// 	this.setData({
	// 	  searchGood: {
	// 	    page: this.data.searchGood.page,
	// 	    size: this.data.searchGood.size,
	// 	    pages: pages
	// 	  }
	// 	})
	//   }).catch(err => {
	// 		// console.log(err)
	//     wx.showToast({
	//       title: err.message,
	//       icon: 'none'
	//     })
	//   })
	// 		// console.log(this.data.goodList,111)
	//   this.setData({
	//     isRequest: true
	//   })
	// },

  specialGoodList() {
    var source = {
      page: this.data.searchGood.page,
      size: this.data.searchGood.size,
    }
    api.post(specialGoodList.list, source).then(res => {
      var list = this.data.goodList.concat(res.rows)
      this.setData({
        goodList: list,
      })
      for (var i = 0; i < list.length; i++) {
        this.setData({
          datetimeTo: list[i].saleGoodsEndtime
        })
      }
      var pages = res.total / this.data.searchGood.size
      this.setData({
        searchGood: {
          page: this.data.searchGood.page,
          size: this.data.searchGood.size,
          pages: pages
        }
      })
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  },

	specialDetail(e){
		// console.log(e)
		if(this.data.timeLeft!='已结束'){
			wx.navigateTo({
				url: '../good-detail/good-detail?goodId='+e.currentTarget.dataset.goodid
			})
		}else{
			wx.showToast({
			  title: '活动已结束',
			  icon: 'none',
			  duration: 2000
			})
		}
			
	},
	 onPullDownRefresh: function() {
	 	wx.showNavigationBarLoading() //在标题栏中显示加载
		this.setData({
			goodList:[],
			searchGood: {
			  page: 1,
			  size: 10,
			  pages:0
			},
		})
	 	this.specialGoodList()
	 	setTimeout(function() {
	 		wx.hideNavigationBarLoading() //完成停止加载
	 		wx.stopPullDownRefresh() //停止下拉刷新
	 	}, 1500);
	 },
		
		
		
	})
